<template>
	<view class="container">
		<NavBar title="选择参与人员" :del='false' />
		<view class="search_page">
			<view v-for="(item,index) in personArr" :key="index" class="item">
				<view class="item_left">
					<checkbox-group class="checkbox_group" :data-item="item" @change='checkboxChange'>
						<label>
							<checkbox value="cb" :checked="item.flag" :color="!allChooseType?'#0b847b':'#3B95FF'" />
						</label>
					</checkbox-group>
					<view class="name">
						{{item.text}}
					</view>
				</view>
				<view class="phone" @click="makePhoneCall(item)">
					{{item.phone}}
				</view>
			</view>
		</view>
		<view class="all_btn">
			<checkbox-group :data-item="item" @change='checkAll'>
				<label>
					<checkbox value="allChoose" :checked="isAllChoose" color="#0b847b" />
					<text v-if='isAllChoose'>
						全不选
					</text>
					<text v-else>
						全选
					</text>
				</label>
			</checkbox-group>
			<button type="warn" style="background: #0b847b;" @click="saveAllPeo" :disabled="!participant.length"
				class="del-btn reset_btn">确 定</button>
		</view>
	</view>
</template>

<script>
	import NavBar from "@/components/NavBar.vue";


	export default {
		components: {
			NavBar,
		},
		data() {
			return {
				participant: [], //选择的用户数组
				isAllChoose: false,
				personArr: [], //党员
			};
		},

		onLoad(options) {
			this.personArr = JSON.parse(options.personnelArr);
			console.log(uni.getStorageSync("participant"));
			// 如果第二次选择(从上个页面返回来),将已经选择的放在数组里
			if (uni.getStorageSync("participant")) {
				let data = JSON.parse(uni.getStorageSync("participant"));
				this.participant = data;

				for (let i = 0; i < data.length; i++) {
					for (let j = 0; j < this.personArr.length; j++) {
						console.log(data[i].phone, this.personArr[j].phone);
						if (data[i].phone == this.personArr[j].phone) {
							this.personArr[j].flag = true;
						}
					}
				}
			}
			this.init();
		},

		methods: {
			// 保存数据
			saveAllPeo() {
				uni.removeStorage({
					key: "participant"
				});
				uni.setStorageSync('participant', JSON.stringify(this.participant));
				uni.navigateBack({
					delta: 1
				});
			},

			// 单条选择
			checkboxChange(e) {
				let phone = e.target.dataset.item.phone;
				let name = e.target.dataset.item.text; // 人名
				let id = e.target.dataset.item.value;
				let flag = e.target.dataset.item.flag;

				// 原来是true,变成false --> 删除
				if (flag) {
					e.target.dataset.item.flag = false;
					for (let i = 0; i < this.participant.length; i++) {
						if (phone == this.participant[i].phone) {
							this.participant.splice(i, 1);
						}
					}
				} else {
					e.target.dataset.item.flag = true;
					this.participant.push({
						id,
						name,
						phone,
						flag: true
					});
				}

				// 全选 状态变更
				if (this.participant.length == this.personArr.length) {
					this.isAllChoose = true;
				} else {
					this.isAllChoose = false;
				}
			},

			// 全选/全不选
			checkAll() {
				this.isAllChoose = !this.isAllChoose;

				// 点击全选, 将所有数据保存；不全选，将所有数据清空
				if (!this.isAllChoose) {
					this.participant = [];
					for (let i = 0; i < this.personArr.length; i++) {
						this.personArr[i].flag = false;
					}
				} else {
					this.participant = [];
					for (let i = 0; i < this.personArr.length; i++) {
						this.personArr[i].flag = true;
						this.participant.push({
							id: this.personArr[i].value,
							name: this.personArr[i].text,
							phone: this.personArr[i].phone,
							flag: this.personArr[i].flag
						});
					}
				}

				// for (let i = 0; i < this.personArr.length; i++) {
				// 	this.personArr[i].flag = this.isAllChoose;
				// }
			},
			// 打电话
			makePhoneCall(item) {
				uni.makePhoneCall({
					phoneNumber: '' + item.phone
				});
			},
			init() {},
		},
	};
</script>

<style>
	@import url("../DJjiaoyuxuexi/common/index.css");

	::v-deep .uni-checkbox-input {
		color: #3B95FF !important;
	}

	page {
		height: 100%;
		overflow-y: scroll;
		background: #F9F9F9;
	}

	.container {
		position: relative;
	}

	.search_page {
		height: 80vh;
		padding-top: 10rpx;
		overflow-y: scroll;
	}

	.item {
		margin: 16rpx auto;
		width: 700rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		box-sizing: border-box;
		background: #fff;
	}

	.item_left {
		display: flex;
	}

	.all_btn {
		width: 100%;
		height: 120rpx;
		background: #f1f1f1;
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-right: 20rpx;
		padding-left: 50rpx;
		box-sizing: border-box;
	}
</style>
